<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'card:zpxsj:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="80%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsCardZpxsjForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { Zpxsj, zpxsjSave, zpxsjForm } from '/@/api/mes/card/zpxsj';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('card.zpxsj');
  const { showMessage } = useMessage();
  const record = ref<Zpxsj>({} as Zpxsj);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增拉手数采') : t('查看拉手数采'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('id'),
      field: 'id',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('条码'),
      field: 'tiaoma',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('型号'),
      field: 'xinghao',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p10日期'),
      field: 'p10rq',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p10加工模式'),
      field: 'p10jgms',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p10结果'),
      field: 'p10jg',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p10打pin位移'),
      field: 'p10dpinwy',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p10打pin位移上限'),
      field: 'p10dpinwysx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p10打pin位移下限'),
      field: 'p10dpinwyxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20日期'),
      field: 'p20rq',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20加工模式'),
      field: 'p20jgms',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p20结果'),
      field: 'p20jg',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p20执行器条码'),
      field: 'p20zxqtm',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝x位置1'),
      field: 'p20lsxyz_1',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝x位置2'),
      field: 'p20lsxy_2',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝x位置3'),
      field: 'p20lsxy_3',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝x位置4'),
      field: 'p20lsxy_4',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝y位置1'),
      field: 'p20lsxy_1',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝y位置2'),
      field: 'p20lsxy_2',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝y位置3'),
      field: 'p20lsxy_3',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝y位置4'),
      field: 'p20lsxy_4',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20x移动速度'),
      field: 'p20xwysd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20y移动速度'),
      field: 'p20ywysd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝扭矩1'),
      field: 'p20lsnj_1',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝扭矩2'),
      field: 'p20lsnj_2',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝扭矩3'),
      field: 'p20lsnj_3',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝扭矩4'),
      field: 'p20lsnj_4',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝扭矩5'),
      field: 'p20lsnj_5',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝扭矩上限'),
      field: 'p20lsnjsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p20螺丝扭矩下限'),
      field: 'p20lsnjxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p30日期'),
      field: 'p30rq',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p30加工模式'),
      field: 'p30jgms',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p30结果'),
      field: 'p30jg',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p30左侧压入深度'),
      field: 'p30zcyrsd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p30左侧压入深度上限'),
      field: 'p30zcyrsdsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p30左侧压入深度下限'),
      field: 'p30zcyrsdxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p30右侧压入深度'),
      field: 'p30ycyrsd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p30右侧压入深度上限'),
      field: 'p30ycyrsdsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p30右侧压入深度下限'),
      field: 'p30ycyrsdxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p40日期'),
      field: 'p40rq',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p40加工模式'),
      field: 'p40jgms',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p40结果'),
      field: 'p40jg',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p40压入深度'),
      field: 'p40yrsd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p40压入深度上限'),
      field: 'p40yrsdsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p40压入深度下限'),
      field: 'p40yrsdxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p40压入力'),
      field: 'p40yrl',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p40压入力上限'),
      field: 'p40yrlsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p40压入力下限'),
      field: 'p40yrlxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50日期'),
      field: 'p50rq',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50加工模式'),
      field: 'p50jgms',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p50结果'),
      field: 'p50jg',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p50左侧压入力'),
      field: 'p50zcyrl',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50左侧压入力上限'),
      field: 'p50zcyrlsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50左侧压入力下限'),
      field: 'p50zcyrlxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50右侧压入力'),
      field: 'p50ycyrl',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50右侧压入力上限'),
      field: 'p50ycyrlsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50右侧压入力下限'),
      field: 'p50ycyrlxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50左侧伺服压入深度'),
      field: 'p50zcsfyrsh',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50左侧伺服压入速度'),
      field: 'p50zcsfyrsd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50右侧伺服压入深度'),
      field: 'p50ycsfyrsh',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p50右侧伺服压入速度'),
      field: 'p50ycsfyrsd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60日期'),
      field: 'p60rq',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60加工模式'),
      field: 'p60hgms',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p60结果'),
      field: 'p60jg',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p60把手空载电流'),
      field: 'p60bskjdl',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手空载电流上限'),
      field: 'p60bskjdlsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手堵转电流'),
      field: 'p60bsdzdl',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手堵转电流上限'),
      field: 'p60bsdzdlsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60手柄电动弹出值'),
      field: 'p60sbddtcz',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60手柄电动弹出值上限'),
      field: 'p60sbddtczsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60手柄电动弹出值下限'),
      field: 'p60sbddtczxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手解锁行程'),
      field: 'p60bsjsxc',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手解锁行程上限'),
      field: 'p60bsjsxcsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手解锁行程下限'),
      field: 'p60bsjsxcxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手空载拉力值'),
      field: 'p60bskzllz',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手空载拉力上限'),
      field: 'p60bskzllzsx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手空载拉力下限'),
      field: 'p60bskzllzxx',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手打开时间'),
      field: 'p60bsdksj',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60把手闭合时间'),
      field: 'p60bsbhsj',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60左右伺服拉把手位置'),
      field: 'p60zysflbswz',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60左右伺服移动速度'),
      field: 'p60zysfwysd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60上下伺服拉把手位置'),
      field: 'p60sxsflbswz',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60上下伺服下拉位置'),
      field: 'p60sxsfxwwz',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p60上下伺服下拉速度'),
      field: 'p60sxsfxlsd',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p70日期'),
      field: 'p70rq',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p70加工模式'),
      field: 'p70jgms',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p70结果'),
      field: 'p70jg',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('p70左侧拍照状态'),
      field: 'p70zcpzzz',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('p70右侧拍照状态'),
      field: 'p70ycpzzz',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await zpxsjForm(data);
    record.value = (res.zpxsj || {}) as Zpxsj;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    updateSchema([
      {
        field: 'id',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
    ]);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await zpxsjSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
